//
//  SelfView.m
//  BosidengIphone
//
//  Created by  on 12-1-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SelfView.h"
#import "MyShowDetailViewController.h"
#import "MainViewController.h"
#import "AppDelegate.h"
#import "ASIFormDataRequest.h"

@implementation SelfView
@synthesize mData;
@synthesize mainViewController;
@synthesize mQueue;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        frame = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, 96);
        imageView = [[SyncImageView alloc] init];
        imageView.frame = frame;
        [self addSubview:imageView];
        [imageView release];
        
        imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        imageButton.frame = frame;
        [self addSubview:imageButton];
        [imageButton addTarget:self action:@selector(imageClick:) forControlEvents:UIControlEventTouchUpInside];
        
        frame = CGRectMake(frame.origin.x, 80, frame.size.width, 16);
        infoLabel = [[UILabel alloc] initWithFrame:frame];
        [infoLabel setFont:[UIFont systemFontOfSize:7]];
        infoLabel.textAlignment = UITextAlignmentCenter;
        infoLabel.textColor = [UIColor whiteColor];
        infoLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:infoLabel];
        [infoLabel release];
        
        frame = CGRectMake(27, 106, 42, 24);
        deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        deleteButton.frame = frame;
        [self addSubview:deleteButton];
        [deleteButton addTarget:self action:@selector(deleteImage:) forControlEvents:UIControlEventTouchUpInside];
        
        frame = CGRectMake(38, 38, 20, 20);
        waitView = [[UIActivityIndicatorView alloc] initWithFrame:frame];
        waitView.hidesWhenStopped = YES;
        [self addSubview:waitView];
        [waitView release];
    }
    return self;
}

- (void)imageClick:(id)sender {
    if (mainViewController && mData) {
        MyShowDetailViewController *detailView = [[MyShowDetailViewController alloc] initWithNibName:@"MyShowDetailViewController" bundle:nil];
        detailView.data = mData;
        [(MainViewController *)mainViewController pushViewController:detailView];
        [detailView release];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	//NSLog(@"buttonIndex=%d", buttonIndex);
	if (buttonIndex == 0) {
        [waitView startAnimating];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/ShowDelete.ashx", [AppDelegate serviceUrl]]];
        ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
        [request addPostValue:mData.showID forKey:@"txt_ShowID"];
        [request addPostValue:[AppDelegate getLanguage] forKey:@"txt_Language"];
        [request addPostValue:[AppDelegate getCustom].customId forKey:@"txt_CustomerId"];
        [request setDelegate:self];
        [request setDidFinishSelector:@selector(requestComplete:)];
        [request setDidFailSelector:@selector(requestFailed:)];
        [mQueue addOperation:request];
        [request release];
	}
}

- (void)deleteImage:(id)sender {
    UIAlertView *infoView = [[UIAlertView alloc] 
							 initWithTitle:[AppDelegate getLanguageString:@"myshow_deleteshow_tip"]
							 message:[AppDelegate getLanguageString:@"myshow_deleteshow_message"]
							 delegate:self 
							 cancelButtonTitle:nil 
							 otherButtonTitles:[AppDelegate getLanguageString:@"myshow_deleteshow_ok"], [AppDelegate getLanguageString:@"myshow_deleteshow_cancel"], nil];
	[infoView show];
	[infoView release];
}

- (void)requestComplete:(ASIFormDataRequest *)request {
    [waitView stopAnimating];
    NSString *result = [request responseString];
    //NSLog(@"Post ShowDelete Request Failed! result=%@", result);
    if ([result rangeOfString:@"html"].length > 0) {
		//[self showMessge:@"Regist Failed!"];
        NSLog(@"Post ShowDelete Request Failed! result=%@", result);
        return;
    }
    
	NSArray *resultInfos = [result componentsSeparatedByString:@"|"];
    NSString *resultCode = [resultInfos objectAtIndex:0];
    NSString *resultMessage = [resultInfos objectAtIndex:1];
	if ([@"1" isEqualToString:resultCode]) {
        //[self removeFromSuperview];
        [mData removeLocalImages];
        [mData deleteObject];
        [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(reloadView) userInfo:nil repeats:NO];
        NSLog(@"Post ShowViewAdd Request Success! result=%@", resultMessage);
    } else {
        NSLog(@"Post ShowViewAdd Request Failed! result=%@", resultMessage);
    }
}

- (void)reloadView {
    [[(MainViewController *)mainViewController currentViewController] initView];
}

- (void)requestFailed:(ASIFormDataRequest *)request {
    [waitView stopAnimating];
    NSLog(@"Post ShowViewAdd Request Failed! error=%@", [request error]);
}

- (void)dealloc {
    self.mainViewController = nil;
    [mData release];
    [mQueue release];
    [super dealloc];
}

- (void)loadData:(MyShowData *)data andRequestQueue:(NSOperationQueue *)queue viewController:(id)viewController {
    //NSLog(@"loadData self=%@", self);
    self.mainViewController = viewController;
    self.mData = data;
    self.mQueue = queue;
    NSString *imageUrl = data.smallImage;
    [imageView loadImage:imageUrl andRequestQueue:queue];
    NSInteger status = [data.showStatus intValue];
    if (status == 1) {
        infoLabel.text = [AppDelegate getLanguageString:@"show_my_show_image_pass"];
        infoLabel.backgroundColor = [UIColor colorWithRed:0 green:102.0/255.0 blue:0 alpha:0.75];
        [deleteButton setImage:[UIImage imageNamed:@"Button_Del_Disabled.png"] forState:UIControlStateNormal];
        deleteButton.enabled = NO;
    } else if (status == 2) {
        infoLabel.text = [AppDelegate getLanguageString:@"show_my_show_image_no_pass"];
        infoLabel.backgroundColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:0.75];
        [deleteButton setImage:[UIImage imageNamed:@"Button_Del.png"] forState:UIControlStateNormal];
        deleteButton.enabled = YES;
    } else {
        infoLabel.text = [AppDelegate getLanguageString:@"show_my_show_image_wait"];
        infoLabel.backgroundColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:0.75];
        [deleteButton setImage:[UIImage imageNamed:@"Button_Del.png"] forState:UIControlStateNormal];
        deleteButton.enabled = YES;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
